package com.cloud.bssp.thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @description： 中断
 * @author：weirx
 * @date：2021/11/24 14:05
 * @version：3.0
 */
public class InterruptTest {

    /**
     * 中断sleep
     * @param args
     */
//    public static void main(String[] args) {
//
//        Thread t = new Thread(() -> {
//            System.out.println("打断状态：" + Thread.currentThread().isInterrupted());
//            try {
//                TimeUnit.SECONDS.sleep(10);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("打断后的状态：" + Thread.currentThread().isInterrupted());
//        });
//        t.start();
//        t.interrupt();
//        System.out.println("打断状态：" + t.isInterrupted());
//    }

    /**
     * 中断正常线程
     *
     * @param args
     */
//    public static void main(String[] args) {
//        Thread t = new Thread(() -> {
//            while (true){
//                if(Thread.currentThread().isInterrupted()){
//                    System.out.println("中断状态：" + Thread.currentThread().isInterrupted());
//                    break;
//                }
//            }
//        });
//        t.start();
//        t.interrupt();
//    }

    /**
     * 中断park
     *
     * @param args
     */
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                System.out.println("打断状态：" + Thread.currentThread().isInterrupted());
                System.out.println("park..." + i);
                LockSupport.park();
            }
        });
        t1.start();
        TimeUnit.SECONDS.sleep(1);
        t1.interrupt();
    }
}
